﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Simple.Framework.Configuration;
using Simple.Framework.Ioc;
using Simple.Framework.Logger;
using Simple.Framework.Schedule;

/*********************************************************
 * 命名空间 Simple.BasicNet.Core.Net
 * 接口名称 Host
 * 开发人员：11920
 * 创建时间：2023/4/6 15:33:15
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace Simple.PrettyClient
{
    public class Client: IClient
	{
		IContainer container;
        IClientConfiguration clientConfigution;
		ILogger logger;
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
        public static IClient BuilderClient()
		{
			var client= new Client();
			return client;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public IClient Start()
		{
			var scheduleManager = container.GetService<IScheduleManager>();
			scheduleManager.Start();
			return this;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="ConfigurationPath"></param>
		/// <returns></returns>
		

		
		
	
		/// <summary>
		/// 
		/// </summary>
		/// <param name="SetConfigution"></param>
		/// <returns></returns>
		public IClient Start(Action<IClientConfiguration> SetConfigution)
		{
			clientConfigution =container.GetService<IClientConfiguration>();
			if (SetConfigution!=null)
			{
				SetConfigution.Invoke(clientConfigution);
			}
			
			return Start();
		}
		/// <summary>
		/// 替换容器之后需要重新注册
		/// </summary>
		/// <returns></returns>
		public IClient ReplaceContainer(IContainer container)
		{
			this.container = Container.SetContainer(container);
			Initalization();
			return this;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="action"></param>
		/// <returns></returns>
		/// <exception cref="ArgumentNullException"></exception>
		public IClient Regster(Action<IContainer> action)
		{
			if (action==null)
			{
				throw new ArgumentNullException(nameof(action));
			}
			action.Invoke(container);
			return this;
		}
		/// <summary>
		/// 注册任务然后启动任务
		/// </summary>
		/// <param name="action"></param>
		/// <returns></returns>
		/// <exception cref="ArgumentNullException"></exception>
		/// 
		public IClient RegisterSchedule(Action<IScheduleManager> action)
		{
			if (action==null)
			{
				throw new ArgumentNullException(nameof(action));
			}
			var scheduleManager = container.GetService<IScheduleManager>();
			action.Invoke(scheduleManager);
			return this;
		}
	    /// <summary>
		/// 
		/// </summary>
		private Client()
		{
			container = Container.GetContainer();
			Initalization();
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="container"></param>
		/// <returns></returns>
		private void Initalization()
		{
			string cfgPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ClientConfiguration.json");
			ConfigurationBulder configurationBulder = new ConfigurationBulder(cfgPath);
			var cfg = configurationBulder.GetClientConfigution<ClientConfiguration>();
			clientConfigution = cfg;
			container.RegistSingletonInstance(cfg);
			container.RegisterSingleton<ILogger,Logger>().Autowird();
			container.RegistSingletonInstance<IClientConfiguration,ClientConfiguration>(cfg);

			var cfgg= container.GetService<IClientConfiguration>();
			container.RegisterSingleton<IScheduleManager,ScheduleManager>().Autowird();
			container.Register<IScheduleHandle,ScheduleHandle>().Autowird();
		    logger = container.GetService<ILogger>();
			logger.Info("容器初始化成功!");
		}
	}
}
